package com.devunion.salon.web.action;

import com.devunion.salon.core.enumeration.PaymentMethodHandler;
import com.devunion.salon.persistence.CashInOut;
import com.devunion.salon.persistence.Day;
import com.devunion.salon.persistence.dao.CashInOutDao;
import com.devunion.salon.persistence.dao.DayDao;
import com.devunion.salon.persistence.dao.LocationDao;
import com.devunion.salon.util.DateFormatter;
import com.devunion.salon.web.bean.ChequeBean;
import com.devunion.salon.web.bean.PaymentBean;
import com.devunion.salon.web.form.CheckoutForm;
import com.devunion.salon.web.form.DayForm;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;


/**
 * DOCUMENT ME!
 *
 * @author Timoshenko Alexander
 */
public class DayAction extends CoreAction {
    private DayDao dayDao;
    private LocationDao locationDao;
    private PaymentMethodHandler paymentMethodHandler;
    private CashInOutDao cashInOutDao;

    public void setDayDao(DayDao dayDao) {
        this.dayDao = dayDao;
    }

    public void setLocationDao(LocationDao locationDao) {
        this.locationDao = locationDao;
    }

    public void setPaymentMethodHandler(PaymentMethodHandler paymentMethodHandler) {
        this.paymentMethodHandler = paymentMethodHandler;
    }

    public void setCashInOutDao(CashInOutDao cashInOutDao) {
        this.cashInOutDao = cashInOutDao;
    }

    public ActionForward welcome(ActionMapping actionMapping, ActionForm actionForm,
                                 HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
            throws Exception {
        DayForm dayForm = (DayForm) actionForm;
        String submittedValue = dayForm.getSubmittedValue();

        if (isCancelled(httpServletRequest)) {
            return actionMapping.findForward("cancel");
        }
        if (!StringUtils.isBlank(submittedValue)) {
            if (submittedValue.equalsIgnoreCase(resources.getMessage("day.button.start"))) {
                Day day = new Day();

                day.setCreated(DateFormatter.getSQLDate(dayForm.getHiddenDate()));
                day.setLocation(locationDao.getLocation(getUsername()));
                day.setStart(dayForm.getStartCashMorning());

                dayDao.save(day);
            }

            if (submittedValue.equalsIgnoreCase(resources.getMessage("day.button.end"))) {
                Day day = dayDao.getDay(DateFormatter.getSQLDate(dayForm.getHiddenDate()),
                        getUsername());

                day.setAjustment(dayForm.getAdjustment());
                day.setAmex(dayForm.getTotalAmex());
                day.setEnded(Boolean.TRUE);
                day.setEndOfTheDay(dayForm.getTotalEndOfTheDay());
                day.setMastercard(dayForm.getTotalMastercard());
                day.setPutInEnvelop(dayForm.getPutInEnvelop());
                day.setTotalCash(dayForm.getTotalCachWithStart());
                day.setVisa(dayForm.getTotalVisa());

                dayDao.update(day);
            }

            return actionMapping.findForward("success");
        }

        dayForm.reset();
        Day day = dayDao.getDay(DateFormatter.getSQLDate(dayForm.getHiddenDate()),
                getUsername());
        if (day != null) {
            CheckoutForm checkoutForm = (CheckoutForm) httpServletRequest.getSession()
                    .getAttribute("checkoutForm");
            if ((checkoutForm.getIsStarted() != null) && checkoutForm.getIsStarted()) {
                for (ChequeBean chequeBean : checkoutForm.getTransactions()) {
                    populate(dayForm, chequeBean.getPayment());
                    if (chequeBean.getSecondPayment() != null) {
                        populate(dayForm, chequeBean.getSecondPayment());
                    }
                    dayForm.addDailyTotal(chequeBean.getTotalPriceAndTaxes() * (1 - chequeBean.getDiscount() / 100));
                }
                Collection<CashInOut> caches = cashInOutDao.getCashByDate(DateFormatter
                        .getSQLDate(dayForm.getHiddenDate()), getUsername());
                for (CashInOut cashInOut : caches) {
                    populate(dayForm, new PaymentBean(cashInOut.getPayment()));
                }
            }
            dayForm.setStartDayCach(day.getStart());
        }

        return actionMapping.getInputForward();
    }

    private void populate(DayForm form, PaymentBean paymentBean) {
        if (paymentMethodHandler.containsCard(paymentBean.getMethod())) {
            if (resources.getMessage("modal.add.transaction.payment.amex")
                    .equalsIgnoreCase(paymentBean.getMethod())) {
                form.addTotalAmex(paymentBean.getAmount());
            } else if (resources.getMessage("modal.add.transaction.payment.visa")
                    .equalsIgnoreCase(paymentBean.getMethod())) {
                form.addTotalVisa(paymentBean.getAmount());
            } else if (resources.getMessage("modal.add.transaction.payment.mastercard")
                    .equalsIgnoreCase(paymentBean.getMethod())) {
                form.addTotalMastercard(paymentBean.getAmount());
            }
        } else if (paymentMethodHandler.containsCheck(paymentBean.getMethod())) {
            form.addTotalCheck(paymentBean.getAmount());
        } else if (paymentMethodHandler.containsCach(paymentBean.getMethod())) {
            form.addTotalCach(paymentBean.getAmount());
        } else if (paymentBean.getMethod().equalsIgnoreCase("cashin")) {
            form.addCashIn(paymentBean.getAmount());
        } else if (paymentBean.getMethod().equalsIgnoreCase("cashout")) {
            form.addCashOut(paymentBean.getAmount());
        }
    }
}
